package com.ruoyi.web.controller.weixin;

import com.ruoyi.common.constant.WxConstants;
import com.ruoyi.common.utils.weixin.WeixinUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WxCode;
import com.ruoyi.system.service.IWeixinService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/wx")
public class WechatController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WechatController.class);

    private String prefix = "weixin/";

    @Autowired
    private IWeixinService weixinService;

    /**
     * 微信后台管理首页展示
     * @return
     */
    //@RequiresPermissions("system:wxCode:view")
    @GetMapping("/index")
    public String index(HttpServletRequest req, ModelMap mmap){
        String wxId = req.getParameter("wxId");

        WxCode code = weixinService.getEntityById(Long.valueOf(wxId));
        mmap.put("code",code);

        String url = req.getScheme()+"://"+ req.getServerName()+req.getRequestURI()+"?"+req.getQueryString();

        System.out.println(url);

        return prefix + "/index";
    }


    @RequestMapping(method = RequestMethod.GET)
    protected void get(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* 获取传过来的几个参数 */

        // 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = req.getParameter("signature");
        // 时间戳
        String timestamp = req.getParameter("timestamp");
        // 随机数
        String nonce = req.getParameter("nonce");
        // 随机字符串
        String echostr = req.getParameter("echostr");

        PrintWriter out = resp.getWriter();
        if (WeixinUtil.checkSingature(WeixinUtil.token_cs, signature, timestamp, nonce)) {
            out.println(echostr);
        }

    }

    @RequestMapping(method = RequestMethod.POST)
    public void post(HttpServletRequest request, HttpServletResponse response) {
        // 响应消息
        PrintWriter out = null;
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");

            // 获取小贝口袋的 wx_code ()
            WxCode code = weixinService.getEntityByWxCode(WxConstants.WX_CODE_TEST);

            // 调用核心业务类接收消息、处理消息
            String respMessage = weixinService.newMessageRequest(request,code);

            out = response.getWriter();
            out.print(respMessage);

        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(),e);
        }finally {
            out.close();
            out = null;
        }
    }
}
